package cy.security.app;

import cy.security.app.social.openid.OpenIdAuthenticationSecurityConfig;
import cy.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import cy.security.core.properties.SecurityConstants;
import cy.security.core.properties.SecurityProperties;
import cy.security.core.validate.code.ValidateCodeSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * @Author: ciyuan
 * @Date: 2019/6/12 21:34
 * spring security 资源服务器
 */
@Configuration
@EnableResourceServer
public class MyAuthorizationResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler myAuthenticationFailureHandler;

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;

    @Autowired
    private SpringSocialConfigurer easySocialConfigurer;

    @Autowired
    private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.formLogin()
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler);

            //验证码的安全配置
        http.apply(validateCodeSecurityConfig)
                .and()
                //短信验证码安全配置
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                //社交登录安全配置
                .apply(easySocialConfigurer)
                .and()
                //第三方登录安全配置
                .apply(openIdAuthenticationSecurityConfig)
                .and()
                .authorizeRequests()
                //声明不需要登录验证的接口
                .antMatchers(
                        SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                        SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
                        securityProperties.getBrowser().getLoginPage(),
                        SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/*",
                        securityProperties.getBrowser().getSignUpUrl(),
                        "/user/registered", "/social/signUp",
                        securityProperties.getBrowser().getSession().getSessionInvalidUrl())
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();

    }
}
